package com.sk.server;

import com.sk.mapper.SUserMapper;
import com.sk.pojo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServer {
    @Autowired
    SUserMapper sUserMapper;

    public RespBean login(String tel, String pwd, HttpServletRequest request) {

        //为了安全
        if (tel.length()<=0 || pwd.length()<0){
            return RespBean.error("账号或密码错误,请重试");
        }

        Map<String, Object> map = new HashMap<>();
        map.put("tel", tel);
        map.put("pwd", pwd);
        Map<String, Object> allByUsernameAndPwd = sUserMapper.getAllByUsernameAndPwd(map);



        if (allByUsernameAndPwd == null) {
            return RespBean.error("账号或密码错误,请重试");
        }

        if ("普通用户".equals(allByUsernameAndPwd.get("role").toString())) {
            return RespBean.error("对不起,您不是管理员用户");
        }

        System.out.println(allByUsernameAndPwd);
        HttpSession session = request.getSession();
        session.setAttribute("role", allByUsernameAndPwd.get("role").toString());
        session.setAttribute("rid", allByUsernameAndPwd.get("rid"));
        session.setAttribute("username", allByUsernameAndPwd.get("username").toString());
        return RespBean.success("登录成功");
    }
}
